package task04;

import task04.enteties.User;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * enteties.User: jeetrainee
 * Date: 10/28/13
 * Time: 8:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class SerializeUtil {
    private String filePath;
//    private List<enteties.User> userList;

    private ObjectInputStream inputStream;
    private ObjectOutputStream outputStream;

    public SerializeUtil(String filePath) {
        this.filePath = filePath;
    }

    public User readObjectFromFile() {
        User users = new User();

        try {
            inputStream = new ObjectInputStream(new FileInputStream(filePath));
            users = (User) inputStream.readObject();
            inputStream.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        } catch (ClassNotFoundException e) {
            System.out.println(e.toString());
        }

        return users;
    }

    public void writeObjectToFile(User user) {
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath));
            outputStream.writeObject(user);
            outputStream.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }

    }

    public void writeObjectByParts(User user) throws IOException {
        String path = filePath + "_1";
        outputStream = new ObjectOutputStream(new FileOutputStream(path));

        outputStream.writeUTF(user.getName());
        outputStream.writeUTF(user.getPassword());
        outputStream.writeInt(user.getAge());
        outputStream.writeFloat(user.getWeight());
        outputStream.writeDouble(user.getHeight());
        outputStream.writeBoolean(user.isWorker());

        outputStream.close();

    }

    public User readObjectByParts() throws IOException {
        User user = new User();
        String path = filePath + "_1";

        inputStream = new ObjectInputStream(new FileInputStream(path));

        user.setName(inputStream.readUTF());
        user.setPassword(inputStream.readUTF());
        user.setAge(inputStream.readInt());
        user.setWeight(inputStream.readFloat());
        user.setHeight(inputStream.readDouble());
        user.setWorker(inputStream.readBoolean());

        inputStream.close();
        return user;
    }

    public float getDiffWrite(User user) throws IOException {
        float result = 0;
        long firstWriteTime = 0;
        long secondWriteTime = 0;

        firstWriteTime = System.nanoTime();
        this.writeObjectToFile(user);
        firstWriteTime = System.nanoTime() - firstWriteTime;

        secondWriteTime = System.nanoTime();
        this.writeObjectByParts(user);
        secondWriteTime = System.nanoTime() - secondWriteTime;

        result = (float) ((float) firstWriteTime / (float) secondWriteTime);

        return result;
    }

    public float getDiffRead() throws IOException {
        float result = 0;
        long firstWriteTime = 0;
        long secondWriteTime = 0;

        firstWriteTime = System.nanoTime();
        this.readObjectFromFile();
        firstWriteTime = System.nanoTime() - firstWriteTime;

        secondWriteTime = System.nanoTime();
        this.readObjectByParts();
        secondWriteTime = System.nanoTime() - secondWriteTime;

        result = (float) ((float) firstWriteTime / (float) secondWriteTime);

        return result;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
